﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Core
{
    public static partial class user
    {
        public static dynamic getUserInfo(string username, string myusername, bool god)
        {
            var sql = new Sql.SqlDataContext();
            var u = sql.User.FirstOrDefault(row => row.username.ToLower() == username.ToLower());
            if (u == null) return new
                {
                    success = false,
                    msg = "用户不存在"
                };
            if (myusername == u.username) god = true;
            var us = sql.UserStatistics.First(row => row.username == u.username);
            var problemAccept = sql.Solve.Count(row => row.username == u.username && row.accept > 0);
            var rank = (from u2 in sql.User
                        where !u2.disabled
                        from us2 in sql.UserStatistics
                        where us2.username == u2.username
                        let pac2 = sql.Solve.Count(s2 => s2.username == u2.username && s2.accept > 0)
                        where pac2 > problemAccept || pac2 == problemAccept && us2.submit < us.submit
                        select u2).Count() + 1;
            return new
            {
                success = true,
                u.username,
                u.nickname,
                u.email,
                u.motto,
                regTime = u.regTime.toTimeString(),
                lastLoginTime = u.lastLoginTime.toTimeString(),
                u.disabled,
                u.publicPersonalInfo,
                sid = u.publicPersonalInfo || god ? u.sid : null,
                name = u.publicPersonalInfo || god ? u.name : null,
                school = u.publicPersonalInfo || god ? u.school : null,
                sex = u.publicPersonalInfo || god ? u.sex.toSex() : 0,
                birthday = u.publicPersonalInfo || god ? u.birthday.toDateString() : null,
                rank,
                us.accept,
                us.submit,
                problemAccept
            };
        }
    }
}
